'use strict';
const db = uniCloud.database()
const collection = db.collection('rc-cid')
exports.main = async (event, context) => {
	let res = await collection.where({
		uid: event.uid || 0,
		cid: event.cid || ''
	}).get()
	let uid = await collection.where({
		uid: event.uid || 0
	}).get()
	if (!event.cid) return res
	// console.log(res.data.length == 0 && event.cid)
	if (res.data.length == 0) {
		if (uid.data.length == 0) {
			res = await collection.add({
				cid: event.cid,
				uid: event.uid
			})
		} else {
			res = await collection.doc(uid.data[0]._id).set({
				cid: event.cid,
				uid: event.uid
			})
		}
	} else {
		res = await collection.doc(res.data[0]._id).set({
			cid: event.cid,
			uid: event.uid
		})
	}
	//返回数据给客户端
	return res
};